/*
 * Created by JFormDesigner on Sat Jun 22 22:50:32 CST 2024
 */

package scct.chenhui.forms;

import scct.chenhui.dao.UserDao;
import scct.chenhui.model.User;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author chen
 */
public class PanelUserManager extends JPanel {

    private DefaultTableModel model;
    MainForm main;
    public PanelUserManager(MainForm main) {
        this.main=main;
        initComponents();
        initViwe();
    }

    private void initViwe() {

        model = (DefaultTableModel) tabUser.getModel();
        String[] cols = {"编号", "用户名", "用户密码", "性别", "电话"};
        for (String col : cols) {
            model.addColumn(col);
        }
        getTableData();
    }
    private void getTableData()
    {
        //        清除原有数据
        model.setRowCount(0);
//        重新读取数据
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user : allUser) {
            Object[] row = {user.getId(), user.getUsername(), user.getPassword(), user.getSex(), user.getMobile()};
            model.addRow(row);
        }
//        更新界面显示数据
        model.fireTableDataChanged();
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        JOptionPane.showMessageDialog(null,"添加用户测试");
    }

    private void btnUpdate(ActionEvent e) {
        // 选中行索引，如果未选中返回-1
        int selectedRow = tabUser.getSelectedRow();
        System.out.println(selectedRow);
        if(selectedRow<0)
        {
            JOptionPane.showMessageDialog(null,"请选择数据后修改");
            return;
        }
       int id= Integer.parseInt(model.getValueAt(selectedRow, 0).toString());
        AddUserDialog2 addUserDialog2 = new AddUserDialog2(main,id);
        addUserDialog2.setModal(true);
        addUserDialog2.setVisible(true);
        getTableData();

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - hui chen
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border .EmptyBorder (
        0, 0 ,0 , 0) ,  "JF\u006frmDesi\u0067ner Ev\u0061luatio\u006e" , javax. swing .border . TitledBorder. CENTER ,javax . swing. border .TitledBorder
        . BOTTOM, new java. awt .Font ( "Dialo\u0067", java .awt . Font. BOLD ,12 ) ,java . awt. Color .
        red ) , getBorder () ) );  addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java .
        beans. PropertyChangeEvent e) { if( "borde\u0072" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {

            //---- tabUser ----
            tabUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - hui chen
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
